﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WLive.WLiveObjects
{
    public class Calendar:AGetOp<Calendar>
    {
        public string id;
        [Writable]
        public string name;
        [Writable]
        public string description;
        public DateTime created_time;
        public DateTime updated_time;
        public From from;
        public bool is_default;
        [Writable]
        public string subscription_location;
        public string permissions;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="usrid">USER_ID</param>
        /// <returns>Returns all calendars of user by user_id when parameter is not empty else returns all calendars of current user</returns>
        public static IEnumerable<Calendar> GetCalendars(string usrid)
        {
            if (string.IsNullOrEmpty(usrid))
                usrid = "me";
            try
            {
                return Requester.Request<Calendar>(new RequestObject { url = UrlBuilder1.Build(usrid) + "/calendars" });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Creates new calendar
        /// </summary>
        /// <param name="usrid">USER_ID</param>
        /// <param name="name">Name of calendar</param>
        /// <param name="summary">Summary of calendar</param>
        /// <returns>Returns new calendar object</returns>
        public static Calendar Create(string usrid, string name, string summary)
        {
            if (string.IsNullOrEmpty(usrid))
                usrid = "me";
            string data = "{\"name\": \"" + name + "\",\r\n \"summary\": \"" + summary + "\"}";
            try
            {
                RequestObject ro = new RequestObject { url = UrlBuilder1.Build(usrid)+"/calendars", method = "POST", content_type = "application/json" };
                ro.SetData(data);
                return Requester.Request<Calendar>(ro).ElementAt(0);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Update properties of calendar
        /// </summary>
        public void Update()
        {
            string data = "{\"name\": \"" + name + "\"}";
            try
            {
                RequestObject ro = new RequestObject { url = UrlBuilder1.Build(id), method = "PUT", content_type = "application/json" };
                ro.SetData(data);
                Requester.Request<Calendar>(ro).ElementAt(0);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Subscribe calendar
        /// </summary>
        /// <param name="userid">USER_ID</param>
        /// <param name="name">Name of calendar</param>
        /// <param name="subscribeLocation">Subscribe location</param>
        public static void Subscribe(string userid, string name, string subscribeLocation)
        {
            if (string.IsNullOrEmpty(userid))
                userid = "me";
            string data = "{\"name\": \"" + name + "\", \r\n \"subscription_location\": \""+subscribeLocation+"\"}";
            try
            {
                RequestObject ro = new RequestObject { url = UrlBuilder1.Build(userid) + "/calendars", method = "POST", content_type = "application/json" };
                ro.SetData(data);
                Requester.Request<Calendar>(ro).ElementAt(0);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Delete current calendar
        /// </summary>
        public void Delete()
        {
            try
            {
                Requester.Request<Calendar>(new RequestObject { url = UrlBuilder1.Build(id), method = "DELETE" });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// Gets all events of calendar
        /// </summary>
        /// <returns>Events collection of calendar</returns>
        public IEnumerable<Event> GetEvents()
        {
            try
            {
                return Requester.Request<Event>(new RequestObject { url = UrlBuilder1.Build(id) + "/events" });
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
